package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.api.wemedia.WemediaClient;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.UserAuthMapper;
import com.heima.user.service.UserAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import cn.hutool.core.util.DesensitizedUtil;

import java.util.Date;
import java.util.List;

@Service
public class UserAuthServiceImpl implements UserAuthService {
    @Autowired
    private UserAuthMapper userAuthMapper;
    @Autowired
    private WemediaClient wemediaClient;
    @Autowired
    private ApUserMapper apUserMapper;
    @Override
    public ResponseResult list(AuthDto dto) {
        Page<ApUserRealname> page = new Page<>(dto.getPage(), dto.getSize());
        userAuthMapper.selectPage(page,new LambdaQueryWrapper<ApUserRealname>()
                                .eq(dto.getId() != null,ApUserRealname::getId,dto.getId())
                                .eq(dto.getStatus() != null,ApUserRealname::getStatus,dto.getStatus())
                                .like(StringUtils.hasText(dto.getMsg()),ApUserRealname::getReason,dto.getMsg()));
        List<ApUserRealname> list = page.getRecords();
        //身份信息脱敏
        for (ApUserRealname apUserRealname : list) {
            String idno = apUserRealname.getIdno();
            String idCardNum = DesensitizedUtil.idCardNum(idno,4,4);
            apUserRealname.setIdno(idCardNum);
        }

        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult authFail (AuthDto dto) {
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setId(dto.getId());
        apUserRealname.setStatus(2);
        if (StringUtils.hasText(dto.getMsg())) {
            apUserRealname.setReason(dto.getMsg());
        }
        userAuthMapper.updateById(apUserRealname);
        return ResponseResult.okResult(apUserRealname);
    }

    @Override
    public ResponseResult authPass(AuthDto dto) {
        ApUserRealname apUserRealname = new ApUserRealname();
        apUserRealname.setId(dto.getId());
        apUserRealname.setStatus(9);
        userAuthMapper.updateById(apUserRealname);
        ApUserRealname userRealName = userAuthMapper.selectById(dto.getId());
        //创建自媒体账号
        WmUser wmUser = new WmUser();
        wmUser.setApUserId(userRealName.getUserId());
        wmUser.setApAuthorId(apUserRealname.getId());
        ApUser apUser = apUserMapper.selectById(userRealName.getUserId());
        wmUser.setName(apUser.getName());
        wmUser.setPassword(apUser.getPassword());
        wmUser.setSalt(apUser.getSalt());
        wmUser.setImage(apUser.getImage());
        wmUser.setPhone(apUser.getPhone());
        wmUser.setStatus(9);
        wmUser.setType(0);
        wmUser.setCreatedTime(new Date());
        return wemediaClient.newUser(wmUser);
    }

    /**
     * 通过 id 获取用户信息
     * @param id 用户 id
     * @return 用户基本信息
     */
    @Override
    public ApUser getById(Integer id) {
        return apUserMapper.selectById(id);
    }
}
